package com.alks.function.data.vo.stock;

import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alks.common.utils.excelUtils.nestExcel.annotation.NestExcelCollection;
import com.alks.common.utils.excelUtils.nestExcel.annotation.NestExcelEntity;
import com.alks.common.utils.excelUtils.nestExcel.annotation.NestExcelProperty;
import com.alks.function.data.dto.pcfactoryquery.PartBfInDto;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author MS
 * @since 2025/2/17下午4:06
 */
@Data
public class PartExportVo {

    @NestExcelProperty(name = "部门")
    private String deptName;

    @NestExcelCollection(name = "部门明细")
    private List<DeptVo> deptDetail;

    @Data
    public static class DeptVo {

        @NestExcelProperty(name = "材料分类")
        private String mtlType;

        @NestExcelCollection(name = "类型明细")
        private List<TypeVo> typeDetail;

        @Data
        public static class TypeVo {
            @NestExcelCollection(name = "出入库单号")
            private String slipNo;

            @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
            @NestExcelProperty(name = "出入库时间")
            @DateTimeFormat(value = "yyyy-MM-dd")
            private LocalDateTime slipDate;

            @NestExcelProperty(name = "出入库原因")
            private String slipReason;

            @NestExcelProperty(name = "材料颜色")
            private String mtlColor;

            @NestExcelProperty(name = "材料名称")
            private String mtlName;

            @NestExcelProperty(name = "材料规格")
            private String mtlSpec;

            @NestExcelProperty(name = "材料单位")
            private String mtlUnit;

            @NestExcelProperty(name = "数量")
            private BigDecimal qty;

            @NestExcelProperty(name = "单价")
            private BigDecimal price;

            @NestExcelProperty(name = "金额")
            private BigDecimal amt;
        }
    }

    public static List<PartExportVo> aggregateParts(List<PartBfInDto> partBfInDtoList) {
        // 按部门名称分组
        Map<String, List<PartBfInDto>> groupedByDept = partBfInDtoList.stream()
                .collect(Collectors.groupingBy(PartBfInDto::getDeptName));

        List<PartExportVo> partExportVoList = new ArrayList<>();

        for (Map.Entry<String, List<PartBfInDto>> entry : groupedByDept.entrySet()) {
            String deptName = entry.getKey();
            List<PartBfInDto> parts = entry.getValue();

            // 按材料类型分组
            Map<String, List<PartBfInDto>> groupedByMtlType = parts.stream()
                    .collect(Collectors.groupingBy(PartBfInDto::getMtlType));

            List<PartExportVo.DeptVo> deptDetailList = new ArrayList<>();

            for (Map.Entry<String, List<PartBfInDto>> typeEntry : groupedByMtlType.entrySet()) {
                String mtlType = typeEntry.getKey();
                List<PartBfInDto> typeParts = typeEntry.getValue();

                List<PartExportVo.DeptVo.TypeVo> typeDetailList = typeParts.stream()
                        .map(part -> {
                            PartExportVo.DeptVo.TypeVo typeVo = new PartExportVo.DeptVo.TypeVo();
                            typeVo.setSlipNo(part.getSlipNo());
                            typeVo.setSlipDate(part.getSlipDate());
                            typeVo.setSlipReason(part.getSlipReason());
                            typeVo.setMtlColor(part.getMtlColor());
                            typeVo.setMtlName(part.getMtlName());
                            typeVo.setMtlSpec(part.getMtlSpec());
                            typeVo.setMtlUnit(part.getMtlUnit());
                            typeVo.setQty(part.getQty());
                            typeVo.setPrice(part.getPrice());
                            typeVo.setAmt(part.getAmt());
                            return typeVo;
                        }).collect(Collectors.toList());
                /*补充类型合计*/
                PartExportVo.DeptVo.TypeVo typeVo = new PartExportVo.DeptVo.TypeVo();
                typeVo.setMtlName("分类合计");
                BigDecimal amtSum = typeDetailList.stream().
                        filter(Objects::nonNull)
                        .map(DeptVo.TypeVo::getAmt)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                typeVo.setAmt(amtSum);
                typeDetailList.add(typeVo);
                PartExportVo.DeptVo deptVo = new PartExportVo.DeptVo();
                deptVo.setMtlType(mtlType);
                deptVo.setTypeDetail(typeDetailList);
                deptDetailList.add(deptVo);
            }
            /*补充部门合计*/
            PartExportVo.DeptVo deptVo = new PartExportVo.DeptVo();
            deptVo.setMtlType("部门合计");
            BigDecimal amtSum = BigDecimal.ZERO;
            for (DeptVo vo1 : deptDetailList) {
                for (DeptVo.TypeVo vo2 : vo1.getTypeDetail()) {
                    if ("分类合计".equals(vo2.getMtlName())) {
                        amtSum = amtSum.add(vo2.getAmt());
                    }
                }
            }
            PartExportVo.DeptVo.TypeVo typeVo = new PartExportVo.DeptVo.TypeVo();
            typeVo.setAmt(amtSum);
            deptVo.setTypeDetail(new ArrayList<>());
            deptVo.getTypeDetail().add(typeVo);
            deptDetailList.add(deptVo);
            PartExportVo partExportVo = new PartExportVo();
            partExportVo.setDeptName(deptName);
            partExportVo.setDeptDetail(deptDetailList);
            partExportVoList.add(partExportVo);
        }

        return partExportVoList;
    }
}
